package com.zn.net
{
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;

    /**
     *rtmp连接
     * @author zn
     *
     */
    public class RtmpNet extends NetConn
    {
        protected static var _instance:RtmpNet;

        public static var URL:String = "";

        public function RtmpNet(url:String = null)
        {
            super(url);
            client = this;
        }

        public static function getInstance():RtmpNet
        {
            if (_instance == null)
                _instance = new RtmpNet(URL);

            return _instance as RtmpNet;
        }

        //key:int 	,	value:array:Function
        private static var _protocolDic:Dictionary = new Dictionary();

        /**
         *注册协议
         * @param commandId
         * @param callFunction
         *
         */
        public static function registerProtocol(commandId:int, callFunction:Function):void
        {
            if (!_protocolDic[commandId])
                _protocolDic[commandId] = new Vector.<Function>();

            _protocolDic[commandId].push(callFunction);
        }

        /**
         *删除协议
         * @param commandId
         *
         */
        public static function deleteProtocol(commandId:int):void
        {
            delete _protocolDic[commandId];
        }

        /**
         * 注销协议中的方法
         * @param commandId
         * @param callFunction
         *
         */
        public static function deleteProtocolFunction(commandId:int, callFunction:Function):void
        {
            if (_protocolDic[commandId])
            {
                var v:Vector.<Function> = Vector.<Function>(_protocolDic[commandId]);

                var index:int = v.indexOf(callFunction);
                if (index != -1)
                    v.splice(index, 1);
            }
        }

        /**
         *根据协议获取注册的方法列表
         * @param commandId
         * @return
         *
         */
        public static function getProtocolFunctionList(commandId:int):Vector.<Function>
        {
            if (_protocolDic[commandId])
                return Vector.<Function>(_protocolDic[commandId]);
            else
                return new Vector.<Function>();
        }

        /**
         *服务器回调方法
         * @param value
         *
         */
        public function ServerToClient(value:Object):void
        {
            if (!value.hasOwnProperty("type") || !value.hasOwnProperty("value"))
                return;

            var type:int = value["type"];
            var data:* = value["value"];

            //克隆对象
            var by:ByteArray = new ByteArray();
            by.writeObject(data);

            //执行方法
            var callFunctionList:Vector.<Function> = getProtocolFunctionList(type);
            for each (var callFun:Function in callFunctionList)
            {

                by.position = 0;
                //调用方法
                callFun(by.readObject());
            }
        }

        public function rtmpCall(parms:*):void
        {
            super.call("ClientToServer", null, null, parms);
        }


    }
}